﻿using System;
using System.Windows.Forms;
using System;
using System.IO;
using System.Threading;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Drawing;
namespace gitee_toast2
{
    public partial class Form1 : Form
    {
        public string access_token = "";
        public string login = "";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            StreamReader sr = new StreamReader("code.txt");
            access_token = sr.ReadLine();
            login = sr.ReadLine();
            Task task_2 = Task.Run(task_YS);
        }
        public async Task task_YS()
        {
            while (true)
            {
                var Url = "https://gitee.com/api/v5/enterprises/zjmskj/issues?access_token=" + access_token + "&state=all&sort=updated&direction=desc&page=1&per_page=50&assignee="+ login;
                WebRequest wReq = WebRequest.Create(Url);
                WebResponse wResp = wReq.GetResponse();
                Stream respStream = wResp.GetResponseStream();
                using (StreamReader reader = new StreamReader(respStream, Encoding.GetEncoding("UTF-8")))
                {
                    string res = reader.ReadToEnd();
                    JsonSerializerSettings settings = new JsonSerializerSettings();
                    settings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;
                    try
                    {
                        List<Response> responseList = JsonConvert.DeserializeObject<List<Response>>(res, settings);
                        MethodInvoker inv = delegate
                        {
                            lTask.Text = "";
                            lTask.ForeColor = Color.Black;
                            foreach (Response response in responseList)
                            {
                                if (response.issue_state.Equals("待办的"))
                                {
                                    lTask.Text += "(" + response.issue_state + ")" + response.title + "\n";
                                    lTask.ForeColor = Color.Red;
                                }
                            }
                            foreach (Response response in responseList)
                            {
                                if (!response.issue_state.Equals("待办的"))
                                {
                                    lTask.Text += "(" + response.issue_state + ")" + response.title + "\n";
                                }
                            }
                            this.Text = "任务(" + login + ")" + DateTime.Now.ToString("HH:mm:ss");
                        };
                        Invoke(inv);
                    }
                    catch (Exception e)
                    {
                        MethodInvoker inv2 = delegate
                        {
                            lTask.Text = e.ToString();
                        };
                        Invoke(inv2);
                    }
                }
                await Task.Delay(180 * 1000);
            }
        }
    }
}
